package org.softmed.rest.editor.comps.utils.list;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

import org.softmed.rest.editor.EditorUtil;
import org.softmed.rest.editor.FilePathAware;
import org.softmed.rest.editor.FilePathProvider;
import org.softmed.rest.editor.commons.SwingUtil;
import org.softmed.rest.editor.commons.URIProvider;
import org.softmed.rest.editor.comps.SimpleEditor;
import org.softmed.rest.editor.comps.browser.PagedList;
import org.softmed.rest.editor.comps.browser.headers.Header;
import org.softmed.rest.editor.comps.full.ApplicationEditorFull;
import org.softmed.rest.editor.comps.full.HandlerEditorFull;
import org.softmed.rest.editor.comps.full.HttpMethodEditorFull;
import org.softmed.rest.editor.comps.full.MimeTypeHandlerEditorFull;
import org.softmed.rest.editor.comps.full.ModuleEditorFull;
import org.softmed.rest.editor.comps.full.ParameterEditorFull;
import org.softmed.rest.editor.comps.full.ResourceEditorFull;
import org.softmed.rest.editor.comps.full.getDeletePanel;
import org.softmed.rest.editor.config.Application;
import org.softmed.rest.editor.config.Handler;
import org.softmed.rest.editor.config.HttpMethod;
import org.softmed.rest.editor.config.MimeTypeHandler;
import org.softmed.rest.editor.config.Module;
import org.softmed.rest.editor.config.Parameter;
import org.softmed.rest.editor.config.Resource;
import org.softmed.swing.IconManager;

public class ItemList extends JPanel implements FilePathAware {

	URIProvider uriProvider;
	List<SimpleEditor> editors = new ArrayList<SimpleEditor>();
	Map<SimpleEditor, Component> structs = new HashMap<SimpleEditor, Component>();

	Header header;
	private ActionListener listener;
	private PagedList plist;
	private FilePathProvider filePathProvider;

	JPanel content = new JPanel();
	private Color childrenBackground;

	// private MouseListener listener;

	public ItemList(URIProvider uriProvider, ActionListener listener) {
		// this.filePathProvider = filePathProvider;
		this.listener = listener;
		this.uriProvider = uriProvider;
		// SwingUtil.setSize(this, EditorUtil.restBrowserWidth, 540);
		// SwingUtil.setSize(scrollPane, EditorUtil.restBrowserWidth, 530);
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

		content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
		Component indent = Box.createHorizontalStrut(5);
		setBackground(Color.WHITE);
		add(indent);
		add(content);
		add(Box.createHorizontalStrut(5));
		// add(scrollPane);
	}

	public void setPagedList(PagedList plist) throws Throwable {
		removeEditorMouseListener();
		this.plist = plist;
		// listener = null;
		// header = null;
		content.removeAll();
		editors.clear();
		List items = plist.getItems();
		if (items == null || items.size() == 0)
			return;

		int count = 1;
		for (Object object : items) {
			addSimpleNonEnabledEditor(object);
			count++;
		}

		repaint();

		// launchThreadToUpdateEditors();
		// setEditorsMouseListener(listener);

	}

	public void addSimpleNonEnabledEditor(Object object) throws Throwable {
		SimpleEditor editor = null;
		if (object instanceof Application) {
			// setupHeader(new AppHeader());
			editor = new ApplicationEditorFull(uriProvider);
		} else if (object instanceof Module) {
			// setupHeader(new AppHeader());
			editor = new ModuleEditorFull(uriProvider);
		} else if (object instanceof Resource) {
			// setupHeader(new ResourceHeader());
			editor = new ResourceEditorFull(uriProvider);
		} else if (object instanceof HttpMethod) {
			// setupHeader(new HttpHeader());
			editor = new HttpMethodEditorFull(uriProvider);
		} else if (object instanceof MimeTypeHandler) {
			// setupHeader(new MimeTypeHeader());
			editor = new MimeTypeHandlerEditorFull(uriProvider);
		} else if (object instanceof Handler) {
			// setupHeader(new HandlerHeader());
			editor = new HandlerEditorFull(uriProvider);
		} else if (object instanceof Parameter) {
			// setupHeader(new ParameterHeader());
			editor = new ParameterEditorFull(uriProvider);
		}

		editor.setEntity(object);
		editor.setEditable(true);
		content.add(editor);
		Component struct = Box.createVerticalStrut(5);
		structs.put(editor, struct);
		content.add(struct);

		if (childrenBackground != null)
			editor.setFullBackground(childrenBackground);

		repaint();
		editors.add(editor);

		if (editor instanceof getDeletePanel) {
			getDeletePanel getDeletePanel = (getDeletePanel) editor;
			JButton remove = new JButton(IconManager.redMinus);
			remove.setActionCommand("remove");
			remove
					.setToolTipText("Remove this object from the parent's list. Does not delete this object.");
			remove.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			// long index = (plist.getCurrentPageNumber() - 1)
			// * plist.getItemsPerPage() + count;
			// remove.setName("" + index);
			remove.addActionListener(listener);
			SwingUtil.setSize(remove, EditorUtil.buttonHeightAndWidth,
					EditorUtil.buttonHeightAndWidth);
			getDeletePanel.getDeletePanel().add(remove);
			getDeletePanel.getDeletePanel().setActionListeners(listener);
		}

		if (editor instanceof showListFieldDetails) {
			showListFieldDetails temp = (showListFieldDetails) editor;
			temp.hideListFieldDetails();
		}

		if (editor instanceof FilePathAware) {
			FilePathAware temp = (FilePathAware) editor;
			temp.setFilePathProvider(filePathProvider);
		}

	}

	// private void setupHeader(Header header) {
	// if (this.header != null)
	// return;
	//
	// this.header = header;
	// add(header);
	//
	// }

	// private void launchThreadToUpdateEditors() {
	// new Thread(new RefreshRunnable(editors)).start();
	// }

	// public void setEditorsMouseListener(MouseListener listener) {
	// this.listener = listener;
	// for (SimpleEditor editor : editors) {
	// editor.addMouseListener(listener);
	// }
	// }
	//
	public void removeEditorMouseListener() {
		for (SimpleEditor editor : editors) {
			if (editor instanceof getDeletePanel) {
				getDeletePanel getDeletePanel = (getDeletePanel) editor;
				getDeletePanel.getDeletePanel().removeActionListeners();

			}
		}
	}

	@Override
	public void setFilePathProvider(FilePathProvider provider) {
		this.filePathProvider = provider;
		for (SimpleEditor editor : editors) {
			if (editor instanceof FilePathAware) {
				FilePathAware temp = (FilePathAware) editor;
				temp.setFilePathProvider(provider);

			}
		}

	}

	public void setFullBackground(Color color) {
		setBackground(color);
		content.setBackground(color);
	}

	public void setChildrenEditorBackground(Color color) {
		childrenBackground = color;

	}

	public List<SimpleEditor> getEditors() {
		return editors;
	}

	public void removeEditor(SimpleEditor editor) {

		content.remove(structs.get(editor));
		content.remove(editor);
		editors.remove(editor);
		structs.remove(editor);
		if (editor instanceof getDeletePanel) {
			getDeletePanel getDeletePanel = (getDeletePanel) editor;
			getDeletePanel.getDeletePanel().removeActionListeners();
		}
	}
}
